<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>添加库存申请单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" />
    <link rel="stylesheet" href="/static/ui/css/ui.css" />
</head>

<body>
    <form class="layui-form" action="" lay-filter="editform">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">选择商品</label>
                        <div class="layui-input-block">
                            <input type="text" name="goods_name" id="goods_name" readonly placeholder="点击选择商品"
                                class="layui-input">
                            <input type="hidden" name="in_id" id="in_id" />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">申请数量</label>
                        <div class="layui-input-block">
                            <input type="number" name="num" id="num" lay-verify="required|len" autocomplete="off" min="1"
                                placeholder="请输入申请数量" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">手写签名</label>
                        <div class="layui-input-block">
                            <span id="signBtn" class="pear-btn pear-btn-primary pear-btn-sm">
                                开始签名
                            </span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <img src="" width="100%" id="imgSignature" >
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="stock_apply-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
            </div>
        </div>
    </form>
    <script src="../config.js"></script>
    <script src="/static/layui/layui.js"></script>
    <script src="/static/ui/ui.js"></script>
    <script>
        layui.use(['form', 'layer', 'tree'], function () {
            let form = layui.form
                , layer = layui.layer
                , $ = layui.$
                , req = _req()
                , aid = req.id;
                form.verify({
					len: (val, item) => {
						let min = item.getAttribute("min")
						if (val < min) {
							return '不能小于' + min;
						}
					}
				})
            $("#goods_name").click(e => {
                layer.open({
                    type: 2,
                    title: '选择商品',
                    shade: 0.1,
                    offset: 'rt',
                    area: ['80%', '100%'],
                    anim: 1,
                    maxmin: true,
                    content: './goods.html'
                });
            })
            if(aid) {
                _get(layui, 'stock_apply/userEditBefore?aid=' + aid, res => {
                    form.val('editform', res)
                    $("#imgSignature").attr('src', res.sign)
                })
            }
            //监听提交
            form.on('submit(stock_apply-save)', function (data) {
                data = data.field;
                let postUrl = 'stock_apply/userAdd';
                
                let postdata = {
                    in_id : $("#in_id").val(),
                    pic : $("#imgSignature").attr('src'),
                    num : $('#num').val()*1
                }
                if(aid) {
                    postUrl = 'stock_apply/userEdit';
                    postdata.id = aid;
                }
                if(!postdata.in_id) {
                    layer.msg('请选择商品');
                    return false;
                }
                if(!postdata.pic) {
                    layer.msg('请签名');
                    return false;
                }
                //console.log(postdata)
                //return;
                _post(layui, postUrl, postdata, res => {
                    //console.log(res)
                    parent.layui.table.reload("stock_apply-table");
                    parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                })
                return false;
            });
            window.showGoods = function(obj) {
                //console.log(obj)
                $("#goods_name").val(obj.data.goods_name)
                $("#in_id").val(obj.data.id)
            }
            $("#signBtn").click(e => {
                layer.open({
                    type: 2,
                    title: '手写签名',
                    shade: 0.1,
                    offset: 'rt',
                    area: ['95%', '100%'],
                    anim: 1,
                    maxmin: true,
                    content: '/libs/sign/index.html'
                });
            })

        });
    </script>

</body>

</html>